package com.dmy.chat.server.netty.handler;

import com.dmy.chat.server.netty.ChannelManager;
import com.dmy.chat.server.netty.ChannelSender;
import com.dmy.chat.server.netty.IChannelManager;
import com.dmy.chat.server.netty.UserEventHandler;
import com.dmy.chat.server.netty.message.Messages;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.AttributeKey;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * create by huahuadelei for 2020/1/4
 */
public class MyHttpHandler extends SimpleChannelInboundHandler<Messages> {
    private final IChannelManager channelManager = ChannelManager.getInstance();
    private final UserEventHandler userEventHandler =  DefaultUserEventHandler.getInstance(DefaultUserChannelBind.getInstance(channelManager));

    /**
     * 读操作
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Messages msg) throws Exception {
        userEventHandler.handler(ctx.channel(),msg);
    }

    /**
     *  客户端连接
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        channelManager.addClient(ctx.channel());
        System.out.println("channelRegistered");
        super.channelRegistered(ctx);
    }

    /**
     * 客户端断开
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelUnregistered");
        super.channelUnregistered(ctx);
    }

    /**
     *
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelActive");
        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelInactive");
        super.channelInactive(ctx);
    }

    /**
     *  读取操作
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelReadComplete");
        super.channelReadComplete(ctx);
    }

    /**
     *  用户事件
     * @param ctx
     * @param evt
     * @throws Exception
     */
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        System.out.println("userEventTriggered");
        super.userEventTriggered(ctx, evt);
    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelWritabilityChanged");
        super.channelWritabilityChanged(ctx);
    }

    /**
     *  异常拦截
     * @param ctx
     * @param cause
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("exceptionCaught");
        super.exceptionCaught(ctx, cause);
    }
}
